package com.xhl.common.algorithm;

import java.util.Stack;

/**
 * 字符串反转 利用了 栈Stack的特征：先进后出  后进先出
 * @Author CazCen
 * @Date 2021/12/5 16:53
 */
public class StringInversion {

    /**
     * 字符串反转
     * @param string 源字符串
     * @return 反转之后的字符串
     */
    public static String reverse(String string){
        
        char[] chars = string.toCharArray();
        Stack stack = new Stack();
        for (int i = 0; i < chars.length; i++) {
            stack.push(chars[i]);
        }
        int length = stack.size();
        StringBuffer reverseString = new StringBuffer();
        for (int i = 0; i < length; i++) {
            reverseString.append(stack.pop());
        }
        return reverseString.toString();
    }

}
